/**
*      author: Y_Yao
*      created: 2025-05-10 16:27:11
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 1e4 + 7;
int n;
int len[N];
int bin[N];
int f[N];
vector<int> E[N];

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        int num;
        cin >> num;
        cin >> len[i];
        int x;
        while(cin >> x)
        {
            if(x == 0) break;
            E[x].push_back(num);
            bin[num]++;
        }
    }

    f[1] = len[1];
    queue<int> q;
    for(int i = 1;i <= n;i++)
    {
        if(bin[i] == 0)
        {
            q.push(i);
            f[i] = len[i];
        }
    }

    while(!q.empty())
    {
        auto t = q.front();
        q.pop();

        for(int i = 0;i < E[t].size();i++)
        {
            int next = E[t][i];
            bin[next]--;
            if(bin[next] == 0)
            {
                q.push(next);
            }
            f[next] = max(f[next],f[t] + len[next]);
        }
    }

    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        ans = max(ans,f[i]);
    }

    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}